package com.plsjavaweb.dao.implementation;

import org.apache.log4j.Logger;

import com.plsjavaweb.dao.DAOFactory;
import com.plsjavaweb.dao.interfaces.AccountDAO;
import com.plsjavaweb.dao.mapper.AccountMapper;
import com.plsjavaweb.dao.utilities.DAO;
import com.plsjavaweb.logging.LoggingConstants;
import com.plsjavaweb.pojo.AccountPOJO;

public class AccountDAOImpl implements AccountDAO {

	static Logger log= Logger.getLogger(DAOFactory.class);

	@Override
	public AccountPOJO getAccountByUsername(String username) {
		log.info(LoggingConstants.entryMessage);
		AccountPOJO account = null;
		String query= " SELECT A.employee_id, A.username, A.password, A.role FROM  employee_account A WHERE A.username = '" + username + "';";
		log.info("created query string");
		
		DAO<AccountPOJO> dao = new DAO<>();
		AccountMapper acctMapper=new AccountMapper();
		
		try {
			log.info(LoggingConstants.tryMessage);
			account = dao.ExecuteQuerySingle(query, acctMapper);
		} catch (Exception e) {
			log.info(LoggingConstants.catchMessage);
			log.error(e.getMessage());
			account = null;
		}
		finally{
			log.info(LoggingConstants.returnedMessage);			
			return account;
		}

		// how to use DAO Implementation
		//get a reference to AccountDAO object
		//AccountDAO actDAO = com.plsjavaweb.dao.DAOFactory.getAccountDAO();
		//actDAO.getAccountByUsername("username");
	}

}
